package com.android.library.wedgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.opengl.Visibility;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import textsvn.mylibrary.R;


/**
 * @author xiaoxing
 * Created by pengsheng-2 on 2018/9/4.
 * 统一TitleBar
 */

public class TitleBarView extends RelativeLayout {
    private RelativeLayout layoutLeft, layoutRight;
    private TextView tvLeft, tvTitle, tvRight;
    private ImageView ivLeft, ivRight;
    private RelativeLayout toolbar;
    private TitleBarClickListener titleBarClickListener;


    public void setTitleBarClickListener(TitleBarClickListener titleBarClickListener) {
        this.titleBarClickListener = titleBarClickListener;
    }

    public TitleBarView(Context context) {
        this(context, null);
    }

    public TitleBarView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TitleBarView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.header_bar_base, this);
        initView();
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.TitleBarView, defStyleAttr, 0);
        int count = array.getIndexCount();
        for(int i = 0; i < count; i++){
            int attr = array.getIndex(i);
            if (attr == R.styleable.TitleBarView_leftTextColor) {
                tvLeft.setTextColor(array.getColor(attr, Color.BLACK));
            } else if (attr == R.styleable.TitleBarView_leftDrawable) {
                ivLeft.setImageResource(array.getResourceId(attr, R.mipmap.icon_back_black));
            } else if (attr == R.styleable.TitleBarView_leftText) {
                tvLeft.setText(array.getString(attr));
            } else if (attr == R.styleable.TitleBarView_centerTextColor) {
                tvTitle.setTextColor(array.getColor(attr, Color.BLACK));
            } else if (attr == R.styleable.TitleBarView_centerTitle) {
                tvTitle.setText(array.getString(attr));
            } else if (attr == R.styleable.TitleBarView_rightDrawable) {
                ivRight.setImageResource(array.getResourceId(attr, 0));
            } else if (attr == R.styleable.TitleBarView_rightText) {
                tvRight.setText(array.getString(attr));
            } else if (attr == R.styleable.TitleBarView_rightTextColor) {
                tvRight.setTextColor(array.getColor(attr, Color.BLACK));
            } else if (attr == R.styleable.TitleBarView_leftImageVisibility) {
                ivLeft.setVisibility(array.getInt(attr, VISIBLE));
            } else if (attr == R.styleable.TitleBarView_rightImageVisibility) {
                ivRight.setVisibility(array.getInt(attr, GONE));
            } else if (attr == R.styleable.TitleBarView_toolbarColor) {
                toolbar.setBackgroundColor(array.getColor(attr, Color.WHITE));
            } else if (attr == R.styleable.TitleBarView_leftTextSize) {
                tvLeft.setTextSize(TypedValue.COMPLEX_UNIT_PX,array.getDimensionPixelSize(attr, 15));
            } else if (attr == R.styleable.TitleBarView_rightTextSize) {
                tvRight.setTextSize(TypedValue.COMPLEX_UNIT_PX,array.getDimensionPixelSize(attr, 15));
            }else if (attr == R.styleable.TitleBarView_centerTitleSize) {
                tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX,array.getDimensionPixelSize(attr, 17));
            }
        }
        array.recycle();

        layoutLeft.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (titleBarClickListener != null) {
                    //防止空数据点击
                    if (ivLeft.getVisibility() == VISIBLE || !TextUtils.isEmpty(tvLeft.getText())){
                        titleBarClickListener.leftClick();
                    }
                }
            }
        });
        layoutRight.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if(titleBarClickListener != null) {
                    titleBarClickListener.rightClick();
                }
            }
        });
    }

    private void initView() {
        tvLeft = findViewById(R.id.tv_left);
        tvTitle = findViewById(R.id.tv_title);
        tvRight = findViewById(R.id.tv_right);
        ivLeft = findViewById(R.id.iv_left);
        ivRight = findViewById(R.id.iv_right);
        layoutLeft = findViewById(R.id.layout_left);
        layoutRight = findViewById(R.id.layout_right);
        toolbar = findViewById(R.id.toolbar);
        tvLeft.setVisibility(VISIBLE);
    }


    /**
     * 设置标题
     */
    public void setTitle(String title) {
        if (!TextUtils.isEmpty(title)) {
            tvTitle.setText(title);
        }
    }

    /**
     * 返回布局
     * @return title布局
     */
    public TextView getTitle(){
        return tvTitle;
    }

    /**
     * 设置左标题
     */
    public void setLeftText(String title) {
        if (title != null) {
            tvLeft.setText(title);
            tvLeft.setVisibility(VISIBLE);
        }
    }

    /**
     * 设置右标题
     */
    public void setRightText(String title) {
        if (!TextUtils.isEmpty(title)) {
            tvRight.setText(title);
            tvRight.setVisibility(VISIBLE);
        }
    }

    /**
     * 设置标题大小
     */
    public void setTitleSize(int size) {
        if (tvTitle != null) {
            tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
        }
    }

    /**
     * 设置左标题大小
     */
    public void setLeftTextSize(int size) {
        if (tvLeft != null) {
            tvLeft.setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
        }
    }

    /**
     * 设置右标题大小
     */
    public void setRightTextSize(int size) {
        if (tvRight != null) {
            tvRight.setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
        }
    }

    /**
     * 设置右标题颜色
     */
    public void setRightTextColor(int color){
        if (tvRight != null){
            tvRight.setTextColor(color);
        }
    }

    /**
     * 设置作标题颜色
     */
    public void setLeftTextColor(int color){
        if (tvLeft != null){
            tvLeft.setTextColor(color);
        }
    }


    /**
     * 设置左图标
     */

    public void setLeftDrawable(int res) {
        if (ivLeft != null) {
            ivLeft.setImageResource(res);
        }
    }

    /**
     * 设置右图标
     */
    public void setRightDrawable(int res) {
        if (ivRight != null) {
            ivRight.setImageResource(res);
            ivRight.setVisibility(VISIBLE);
        }
    }

    /**
     * 隐藏左边文字
     */
    public void hitIvLeft(){
        if (ivLeft != null){
            ivLeft.setVisibility(GONE);
        }
    }

    /**
     * 获取右边textView
     */
    public TextView getTvRight(){
        return tvRight;
    }

    /**
     * 获取左边image
     */
    public ImageView getIvLeft(){
        return ivLeft;
    }

    public interface TitleBarClickListener {
        /**
         * 左边点击事件
         */
        void leftClick();

        /**
         * 右边点击事件
         */
        void rightClick();
    }
}
